package mq

import (
	"emicro_test/emicro_7/grpc_server/user/common"
	RabbimqPool "gitee.com/tym_hmm/rabbitmq-pool-go"
	"log"
)

var PubMqPool *RabbimqPool.RabbitPool

func InitRabbitmq() {
	PubMqPool = RabbimqPool.NewProductPool()
	PubMqPool.SetMaxConnection(4)
	rabAddr := common.Config.String("rabmq_addr")
	rabPort, _ := common.Config.Int("rabmq_port")
	rabUser := common.Config.String("rabmq_user")
	rabPwd := common.Config.String("rabmq_pwd")
	err:= PubMqPool.ConnectVirtualHost(rabAddr, rabPort, rabUser, rabPwd, "/")
	if err != nil {
		log.Println("InitRabbitmq Err:", err)
	}
}

//发布消息
func Publish(args ...string) *RabbimqPool.RabbitMqError{
	if len(args) < 4 {
		return &RabbimqPool.RabbitMqError{Message: "args error"}
	}

	var exChangeType string
	exChangeName := args[0]
	queueName := args[1]
	routeKey := args[2]
	body := args[3]

	if len(args) == 4 {
		exChangeType = RabbimqPool.EXCHANGE_TYPE_DIRECT
	}else {
		exChangeType = args[4]
	}

	data := RabbimqPool.GetRabbitMqDataFormat(
		exChangeName,
		exChangeType,
		queueName,
		routeKey,
		body,
	)
	return PubMqPool.Push(data)
}
